package com.compty.paint.web.front;

import java.util.List;

import com.compty.core.framework.util.StringUtil;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.AddressInfo;
import com.compty.paint.domain.CityInfo;
import com.compty.paint.domain.CustomerInfo;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.Order;
import com.compty.paint.domain.ProvinceInfo;
import com.compty.paint.exception.OrderException;
import com.compty.paint.manager.AddressInfoManager;
import com.compty.paint.manager.CityInfoManager;
import com.compty.paint.manager.CustomerInfoManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.manager.OrderManager;
import com.compty.paint.manager.ProvinceInfoManager;
import com.compty.paint.util.Constants;

/**
 * 处理用户订购
 * @author 朱庆辉
 * @CreateTime:2012-8-13 下午02:33:23
 */
public class OrderAction   extends BaseAction<Order> {

	private static final long serialVersionUID = 8028427045115698133L;
	
	private GoodsManager goodsManager;
	private OrderManager orderManager;
	private transient ProvinceInfoManager provinceInfoManager;
	private transient CityInfoManager cityInfoManager;
	private transient CustomerInfoManager customerInfoManager;
	private transient AddressInfoManager  addressInfoManager;
	
	private Integer orderId;
	
	/**
	 * 检查是否已登录 
	 */
	public String checkL(){
		Object customerInfo =  session.getAttribute(Constants.CustomerInfo);
		if( customerInfo != null){
			request.setAttribute("ajaxData", "0000");
		}else{
			request.setAttribute("ajaxData", "0001");
		}
		return "checkLogin";
	}
	
	/**
	 * 订购prepare
	 */
	public String orderPrepare(){
		try{
			if( session.getAttribute(Constants.CustomerInfo) == null ){
				return "login_min";
			}
			
			String gId = request.getParameter("gId");
			String num = request.getParameter("num");
			if( !StringUtil.isDigit(gId)  ||  !StringUtil.isDigit(num) ){
				return ERROR;
			}
			Integer number = Integer.valueOf(num);
			
			Goods goods = goodsManager.findById(Integer.valueOf(gId));
			
			double real_price = 0.0;
			Double  mktPrice = goods.getMktprice();
			if(mktPrice == null){
				real_price = goods.getPrice();
			}else{
				real_price = mktPrice;
			}
			double totalPrice  = real_price * number;
			
			request.setAttribute("goods", goods);
			request.setAttribute("num", num);
			request.setAttribute("totalPrice", totalPrice);
			
		}catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("returnMsg", "请重新选购商品，再提交订单。<a href='javascript:history.back()'>重新选购</a>");
			return "error";
		}
		return "orderPrepare";
	}
	
	/**
	 * 确认下订单,保存订单
	 */
	public String confirmOrder(){
		try{
			CustomerInfo customerInfo = null;
			if( session.getAttribute(Constants.CustomerInfo) == null ){
				return "login";
			}else{
				customerInfo = (CustomerInfo) session.getAttribute(Constants.CustomerInfo);
				customerInfo = customerInfoManager.get(customerInfo.getId());
			}
			
			String gId = request.getParameter("gId");
			String num = request.getParameter("num");
			
			if( !StringUtil.isDigit(gId)  ||  !StringUtil.isDigit(num) ){
				request.setAttribute("returnMsg", "请先选购商品，再提交订单。");
				return ERROR;
			}
			
			/*************** 发票信息 begin *********************/
			String fpFlag = request.getParameter("fpFlag");
			String fpType = request.getParameter("fpType");
			String fpType2 = request.getParameter("fpType2");
			String fpHead = request.getParameter("fpHead");
			
			Order order = new Order();
			order.setStep("1");//核对订单步骤
			if("1".equals(fpFlag)){
				order.setFpFlag(fpFlag);
				order.setFpType(fpType);
				order.setFpType2(fpType2);
				order.setFpHead(fpHead);
			}else{
				order.setFpFlag("0");
			}
			/*************** 发票信息 end *********************/
			
			orderManager.saveOrder(order,customerInfo,gId,num);//保存订单
			this.orderId = order.getId();
			
			/*************** 更新用户个人订单总量信息   begin *********************/
			if(session.getAttribute("count_total_order") != null){
				Integer totalOrder = (Integer) session.getAttribute("count_total_order");
				session.setAttribute("count_total_order",totalOrder + 1);
			}
			if( session.getAttribute("count_notpay_order") != null){
				Integer notPayOrder = (Integer) session.getAttribute("count_notpay_order");
				session.setAttribute("count_notpay_order",notPayOrder + 1);
			}
			/*************** 更新用户个人订单总量信息   end *********************/
			
		}catch (OrderException e) {
			String msg = e.getMsg();
			request.setAttribute("returnMsg", msg+",<a href='javascript:history.back()'>重新选购</a>");
			return "error";
		}
		return "confirmOrder";
	}
	
	/**
	 * 确认收货信息prepare
	 */
	public String confirmAddressPrepare(){
		
		CustomerInfo customerInfo = null;
		if( session.getAttribute(Constants.CustomerInfo) == null ){
			return "login";
		}else{
			customerInfo = (CustomerInfo) session.getAttribute(Constants.CustomerInfo);
			
			List<AddressInfo>  addressList = addressInfoManager.findByCustomerInfo(customerInfo);
			request.setAttribute("addressList", addressList);
			
			int size = addressList.size();
			request.setAttribute("sizeOfaddressList", size);
		}
		
		List<ProvinceInfo> provinceInfoList = provinceInfoManager.getAll();
		request.setAttribute("provinceInfoList", provinceInfoList);
		
		return "confirmAddressPrepare";
	}
	
	/**
	 * 确认收货信息(添加新收货地址)
	 */
	public String confirm_address_new(){
			
			try {
				CustomerInfo customerInfo = (CustomerInfo) session.getAttribute(Constants.CustomerInfo);
				
				int count_addresses = addressInfoManager.findCountByCustomerInfo(customerInfo);
				if( count_addresses >= 5){
					request.setAttribute("returnMsg","每个会员最多允许添加五个收货地址");
					return "error";
				}
				
				String consignee = request.getParameter("consignee");
				String provinceInfoSelect = request.getParameter("provinceInfoSelect");
				String cityInfoSelect = request.getParameter("cityInfoSelect");
				String address = request.getParameter("address");
				String mobile = request.getParameter("mobile");
				String zipCode = request.getParameter("zipCode");
				
				AddressInfo addressInfo = new AddressInfo();
				addressInfo.setCustomerInfo(customerInfo);
				addressInfo.setConsignee(consignee);
				if(StringUtil.isDigit(provinceInfoSelect)){
					ProvinceInfo provinceInfo = new ProvinceInfo();
					provinceInfo.setId(Long.valueOf(provinceInfoSelect));
					addressInfo.setProvinceInfo(provinceInfo);
				}
				if(StringUtil.isDigit(cityInfoSelect)){
					CityInfo cityInfo = new CityInfo();
					cityInfo.setId(Long.valueOf(cityInfoSelect));
					addressInfo.setCityInfo(cityInfo);
				}
				addressInfo.setAddress(address);
				addressInfo.setMobile(mobile);
				addressInfo.setZipCode(zipCode);
				addressInfoManager.save(addressInfo);
				
				String orderId  = request.getParameter("orderId");
				Integer orderID = Integer.valueOf(orderId);
				Order order = orderManager.get(orderID);
				order.setStep("2");//填写收货地址步骤
				if( order != null ){
					/*************** 保存订单相关信息  begin *************/
					ProvinceInfo provinceInfo  = provinceInfoManager.get(addressInfo.getProvinceInfo().getId());
					CityInfo cityInfo = cityInfoManager.get(addressInfo.getCityInfo().getId());
					
					String shipAddr = provinceInfo.getName()+cityInfo.getName()+ addressInfo.getAddress();
					order.setShip_addr(shipAddr);
					order.setShip_name(addressInfo.getConsignee());
					order.setShip_mobile(addressInfo.getMobile());
					order.setShip_zip(addressInfo.getZipCode());
					order.setAddressInfo_id(addressInfo.getId());
					orderManager.save(order);
					/*************** 保存订单相关信息  end *************/
				}
				
				request.setAttribute("orderid", orderId);
				
			} catch (NumberFormatException e) {
				request.setAttribute("errormessage","保存新收货信息失败。");
				return "error";
			}
		
		return "confirmAddress";
	}
	

	/**
	 * 确认收货信息
	 */
	public String confirmAddress(){
		
		try{
			String orderId  = request.getParameter("orderId");
			String addressChoose  = request.getParameter("addressChoose");
			if( !StringUtil.isDigit(orderId)  ||  !StringUtil.isDigit(addressChoose) ){
				return ERROR;
			}
			
		    if(orderId == null || "".equals(orderId)){
				request.setAttribute("returnMsg", "请先选购商品，再提交订单。<a href='javascript:history.back()'>重新选购</a>");
				return "error";
			}
				
			Integer orderID = Integer.valueOf(orderId);
			Order order = orderManager.get(orderID);
			order.setStep("2");//填写收货地址步骤
			if( order != null ){
				AddressInfo addressInfo = addressInfoManager.get(Integer.valueOf(addressChoose));
				if(addressInfo != null){
					/*************** 保存订单相关信息  begin *************/
					ProvinceInfo provinceInfo  = provinceInfoManager.get(addressInfo.getProvinceInfo().getId());
					CityInfo cityInfo = cityInfoManager.get(addressInfo.getCityInfo().getId());
					
					String shipAddr = provinceInfo.getName()+cityInfo.getName()+ addressInfo.getAddress();
					order.setShip_addr(shipAddr);
					order.setShip_name(addressInfo.getConsignee());
					order.setShip_mobile(addressInfo.getMobile());
					order.setShip_zip(addressInfo.getZipCode());
					order.setAddressInfo_id(addressInfo.getId());
					orderManager.save(order);
					/*************** 保存订单相关信息  end *************/
				}
			}
			request.setAttribute("orderid", orderId);
			
		}catch (Exception e) {
			request.setAttribute("returnMsg", "<a href='javascript:history.back()'>重新选购</a>");
			return "error";
		}
		
		return "confirmAddress";
	}
	
	/**
	 * 列出所有未付订单
	 */
	public String listNotPay(){
		
		CustomerInfo customerInfo = null;
		if( session.getAttribute(Constants.CustomerInfo) == null ){
			return "login";
		}else{
			customerInfo = (CustomerInfo) session.getAttribute(Constants.CustomerInfo);
		}
		
		page = orderManager.listOrderNotPay(curPage,pageSize,customerInfo);
		
		return "listNotPay";
	}
	
	/**
	 * 列出所有订单
	 */
	public String listAll(){
		
		CustomerInfo customerInfo = null;
		if( session.getAttribute(Constants.CustomerInfo) == null ){
			return "login";
		}else{
			customerInfo = (CustomerInfo) session.getAttribute(Constants.CustomerInfo);
		}
		
		page = orderManager.listAllOrder(curPage,pageSize,customerInfo);
		
		return "listAll";
	}

	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	} 
	
	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}

	public void setProvinceInfoManager(ProvinceInfoManager provinceInfoManager) {
		this.provinceInfoManager = provinceInfoManager;
	}

	public void setCityInfoManager(CityInfoManager cityInfoManager) {
		this.cityInfoManager = cityInfoManager;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public void setCustomerInfoManager(CustomerInfoManager customerInfoManager) {
		this.customerInfoManager = customerInfoManager;
	}

	public void setAddressInfoManager(AddressInfoManager addressInfoManager) {
		this.addressInfoManager = addressInfoManager;
	}

}
